package com.hc.daoImpl;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;





import com.hc.bean.UserBean;
import com.hc.dao.UserDao;
import com.hc.util.BaseSql;

public class UserDaoImpl extends BaseSql implements UserDao{

	@Override
	public List<UserBean> getUsers() {
		openConnect();
		List<UserBean> list = new ArrayList<UserBean>();
		String sql = "SELECT id,name,pwd,addTime,lastLoginTime,remark FROM users ";
		java.sql.ResultSet 	rs = null;
		try {
			rs= sta.executeQuery(sql);
			 
			 
			 
			   while (rs.next())
			      {
			    	  
			    	  UserBean bean = new UserBean();
			    	  bean.setId(rs.getInt("id"));
			    	  bean.setName(rs.getString("name"));
			    	  bean.setPwd(rs.getString("pwd"));
			    		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    	 
						bean.setAddTime(rs.getString("addTime").toString());
						  bean.setLastLoginTime(rs.getString("lastLoginTime").toLowerCase());
				
			    	
			    	  bean.setRemark(rs.getString("remark"));
			    	  
			    	 list.add(bean);
			    	  
			   }
			   
	
			   
				
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			
			 try {
				 rs.close();
				sta.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}   
		return list;
	}

	@Override
	public void addUser(UserBean bean) {
		
		openConnect();
	

		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql = "insert into users (name ,pwd , addTime,lastLoginTime,remark) VALUES ( '"+bean.getName()+"' , '"+bean.getPwd()+"' ,'"+formate.format(new Date())+"' ,'"+formate.format(new Date())+"','"+bean.getRemark()+"') ";
		try {
			  sta.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		}  finally{
			
			 try {
				 
				sta.close();
				con.close();
			} catch (SQLException e) {
	
				e.printStackTrace();
			}
	
		}  
	      
	     
	      
		
	}

	@Override
	public void updateUserBean(UserBean bean) {
		
		
		openConnect();
		
//		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
		String sql = "update   users  set name='"+bean.getName()+"' ,pwd = '"+bean.getPwd()+"',lastLoginTime = '"+	bean.getLastLoginTime()+"' ,remark='"+bean.getRemark()+"' where id ="+ bean.getId();
		try {
			  sta.executeUpdate(sql);
			 
			   
				
		} catch (SQLException e) {
			e.printStackTrace();
		}  finally{
			
			 try {
				 
				sta.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}    
		
	}

	@Override
	public void delUserBean(UserBean bean) {
		
		openConnect();
		

		
		String sql = " delete from users  where id = "+ bean.getId();
		try {
			  sta.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		}   finally{
			
			 try {
				 
				sta.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
	}



}
